
package com.stchou.superwidget.slider;

import com.stchou.superwidget.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

public class SliderView extends View
{

    private int START_X = 20;
    private int START_Y = 0;
    private int MOVE_X_PIXEL = 5;
    private Paint mPaint;
    private int m_sliderBtnUpId = R.drawable.slidebtnup;
    private int m_sliderBtnDownId = R.drawable.slidebtndn;
    private Bitmap mSlideBtnImage, mSlideBtnUpImage, mSlideBtnDnImage;
    int movex, fingerInBtn;
    boolean mMoveValid;
    private OnSliderListener m_listener;

    public SliderView(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        initViews(context);
    }

    public SliderView(Context context)
    {
        super(context);
        initViews(context);
    }

    private void initViews(Context context)
    {
        setFocusable(true);
        mSlideBtnUpImage = ((BitmapDrawable)this.getResources().getDrawable(m_sliderBtnUpId)).getBitmap();
        mSlideBtnDnImage = ((BitmapDrawable)this.getResources().getDrawable(m_sliderBtnDownId)).getBitmap();
        mSlideBtnImage = mSlideBtnUpImage;

        movex = START_X; // button first position in X;
        fingerInBtn = 0;
        mMoveValid = false;

        mPaint = new Paint();
    }

    public void setSliderButtonImage(int resId){
        this.m_sliderBtnUpId = resId;
    }
    public void setSliderButtonDownImage(int resId){
        this.m_sliderBtnDownId = resId;
    }
    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        // draw button
        DrawImage(canvas, mSlideBtnImage, 0, 0, mSlideBtnImage.getWidth(), mSlideBtnImage.getHeight(), movex, START_Y);

        // draw Time text
        postInvalidate();
    }

    public void DrawImage(Canvas canvas, Bitmap bitmap, int srcx, int srcy, int w, int h, int desx, int desy)
    {
        Rect src = new Rect(srcx, srcy, srcx + w, srcy + h);
        Rect dest = new Rect(desx, desy, desx + w, desy + h);
        canvas.drawBitmap(bitmap, src, dest, mPaint);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {

        if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT)
        {
            if (this.movex >= START_X + MOVE_X_PIXEL)
                this.movex -= MOVE_X_PIXEL;
        }
        else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
        {
            if (this.movex + this.mSlideBtnImage.getWidth() <= getWidth() - START_X - MOVE_X_PIXEL)
                this.movex += MOVE_X_PIXEL;
        }

        this.postInvalidate();

        return super.onKeyDown(keyCode, event);
    }

    /*
     * The first method to count unlock is that in onTouchEvent. Another method
     * is using GestureDetector to deal with it.
     */
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {

        int action = event.getAction();

        int x = (int)event.getX();
        int y = (int)event.getY();

        if (action == MotionEvent.ACTION_MOVE)
        {
            if (mMoveValid)
            {
                x -= fingerInBtn;

                if (x > getWidth() - START_X + MOVE_X_PIXEL - this.mSlideBtnImage.getWidth())
                    movex = getWidth() - START_X - this.mSlideBtnImage.getWidth();
                else if (x < START_X)
                    movex = START_X;
                else
                    movex = (int)event.getX() - fingerInBtn;
            }
        }
        else if (action == MotionEvent.ACTION_DOWN)
        {

            int topY = START_Y;
            int bottomY = topY + getHeight();
            if ((y > topY) && (y < bottomY) && (x > movex) && (x < movex + mSlideBtnImage.getWidth()))
            {
                mSlideBtnImage = mSlideBtnDnImage;
                fingerInBtn = (int)event.getX() - movex;
                mMoveValid = true;
            }
            else
                mMoveValid = false;
        }
        else if (action == MotionEvent.ACTION_UP)
        {
            mSlideBtnImage = mSlideBtnUpImage;
            movex = START_X;
            mMoveValid = false;
        }

        // redraw with new param
        this.postInvalidate();

        // judge unlock or not
        if (movex == getWidth() - START_X - this.mSlideBtnImage.getWidth())
        {
            if (m_listener != null)
                m_listener.onSliderRight(this);
        }
        super.onTouchEvent(event);
        return true;
    }

    public void setOnSliderListener(OnSliderListener listener)
    {
        this.m_listener = listener;
    }
}
